package cn.tedu.smartos.health.service.impl;

import cn.tedu.smartos.base.exception.ServiceException;
import cn.tedu.smartos.health.mapper.DeviceMapper;
import cn.tedu.smartos.health.pojo.dto.DeviceAddParam;
import cn.tedu.smartos.health.pojo.entity.Device;
import cn.tedu.smartos.health.pojo.vo.DeviceVO;
import cn.tedu.smartos.health.service.DeviceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private DeviceMapper deviceMapper;

    /**
     * 添加设备
     * @param deviceAddParam
     */
    @Override
    public void addDevice(DeviceAddParam deviceAddParam) {
        log.debug("开始处理【添加设备】的请求,参数{}",deviceAddParam);
        Device device = new Device();
        BeanUtils.copyProperties(deviceAddParam,device);
        device.setBindTime(new Date());
        device.setLastActiveTime(new Date());
        Integer rows = deviceMapper.insert(device);
        if (rows != 1) {
            throw new ServiceException("添加设备失败");
        }
    }
    /**
     * 删除设备
     * @param deviceId 设备ID
     */
    @Override
    public void removeDevice(Long deviceId) {
        log.debug("开始处理【删除设备】的请求,参数{}",deviceId);
        if (deviceId == null || deviceId <= 0) {
            throw new IllegalArgumentException("设备ID值无效");
        }
        Integer rows = deviceMapper.delete(deviceId);
        if (rows != 1) {
            throw new ServiceException("删除设备失败");
        }
    }
    /**
     * 修改设备
     * @param pageNum
     * @param pageSize
     */
    @Override
    public PageInfo<DeviceVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询设备列表】的请求,页码:{},每页记录数:{}",pageNum,pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<DeviceVO> deviceVOS = deviceMapper.list();
        return new PageInfo<>(deviceVOS);
    }
    /**
     * 查询设备详情
     * @param deviceId 设备ID
     * @return 设备详情
     */
    @Override
    public DeviceVO detail(Long deviceId) {
        log.debug("开始处理【查询设备详情】的请求,设备ID:{}",deviceId);
        if (deviceId == null){
            throw new ServiceException("参数deviceId不能为空");
        }
        DeviceVO deviceVO = deviceMapper.detail(deviceId);
        return deviceVO;
    }
}
